/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.templates.client.bindings;

import java.util.ArrayList;
import java.util.List;

import org.nuxeo.gwt.habyt.templates.client.Constants;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class Helper implements Constants {

    public static void bindWidget(Widget widget) {
        widget.getElement().setPropertyObject(THIS, widget);
    }

    public static Widget getWidget(Element element) {
        return (Widget) element.getPropertyObject(THIS);
    }

    public static Widget getParent(Element element) {
        Element p = element.getParentElement();
        while (p != null) {
            Widget w = getWidget(p);
            if (w != null) {
                return w;
            }
            p = p.getParentElement();
        }
        return null;
    }

    /**
     * Get the first widget in the element parents hierarchy which have the
     * class name equals to the given type
     * 
     * @param element
     * @param type
     * @return
     */
    public static Widget getParent(Element element, String type) {
        Element p = element.getParentElement();
        while (p != null) {
            Widget w = getWidget(p);
            if (w != null && type.equals(w.getClass().getName())) {
                return w;
            }
            p = p.getParentElement();
        }
        return null;
    }

    public static List<Widget> getParents(Element element) {
        ArrayList<Widget> parents = new ArrayList<Widget>();
        Element p = element.getParentElement();
        while (p != null) {
            Widget w = getWidget(p);
            if (w != null) {
                parents.add(w);
            }
            p = p.getParentElement();
        }
        return parents;
    }

    /**
     * Get the attribute of the given element. First the property is tried then
     * the getAttribute method used. (getAttribute behavior can be different
     * between browsers)
     * 
     * @param element
     * @param name
     * @return
     */
    public static String getAttribute(Element element, String name) {
        String v = element.getPropertyString(name);
        if (v == null || v.length() == 0) {
            v = element.getAttribute(name);
            if (v != null && v.length() == 0) {
                v = null;
            }
        }
        return v;
    }

    public static String getIdAttribute(Element element) {
        String v = element.getId();
        if (v == null || v.length() == 0) {
            return null;
        }
        return v;
    }

    public static String getStyleAttribute(Element element) {
        String v = element.getAttribute("style");
        if (v == null || v.length() == 0) {
            return null;
        }
        return v;
    }

    public static void setSyleAttribute(Element element, String style) {
        element.setAttribute("style", style);
    }

    public static String getWidthAttribute(Element element) {
        String v = element.getAttribute("width");
        if (v == null || v.length() == 0) {
            return null;
        }
        return v;
    }

    public static String getHeightAttribute(Element element) {
        String v = element.getAttribute("height");
        if (v == null || v.length() == 0) {
            return null;
        }
        return v;
    }

    public static String getClassAttribute(Element element) {
        String v = element.getClassName();
        if (v == null || v.length() == 0) {
            return null;
        }
        return v;
    }

    public static void addClassAttribute(Element element, String className) {
        element.addClassName(className);
    }

    /** DOM traversal */

    public static Element getFirstChildElement(Element element, String tagName) {
        Element child = element.getFirstChildElement();
        while (child != null) {
            if (tagName.equalsIgnoreCase(child.getNodeName())) {
                return child;
            }
            child = child.getNextSiblingElement();
        }
        return child;
    }

    public static Element getNextSiblingElement(Element element, String tagName) {
        Element next = element.getNextSiblingElement();
        while (next != null) {
            if (tagName.equalsIgnoreCase(next.getNodeName())) {
                return next;
            }
            next = next.getNextSiblingElement();
        }
        return next;
    }

}
